package com.si.controller.manage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.si.entity.Info;
import com.si.entity.InfoTypeClassA;
import com.si.entity.OperationLog;
import com.si.service.BlogrollService;
import com.si.service.OperationLogService;
import com.si.util.IpAddressUtil;
import com.si.util.Paging;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.BrandActivity;
import com.si.service.BrandActivityService;
import com.si.util.FileUpload;

/**
 * 品牌活动
 *
 * @author x1c
 */
@Controller
@RequestMapping("/brandActivity/manage")
public class BrandActivityManageController {

    @Autowired
    private BrandActivityService brandActivityService;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * @return
     */
    @RequestMapping(value = "list")
    public String list(
            ModelMap modelMap,
            @RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "keywords", required = false) String keywords) {

        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (StringUtils.isNoneBlank(keywords)) {
            parameterMap.put("%cnName%", keywords);
        }
        Map<String, Object> orderMap = new HashMap<String, Object>();
        orderMap.put("createDate", "desc");
        Paging<BrandActivity> paging = brandActivityService.createQueryPaging(parameterMap, orderMap, curPage, pageSize);
        modelMap.put("paging", paging);

        modelMap.put("keywords", keywords);
        return "managePage/brandActivity/list";
    }


    /**
     * 进入到添加页面
     *
     * @return
     */
    @RequestMapping(value = "initAdd")
    public String initAdd(ModelMap modelMap) {
        modelMap.put("message", "");

        return "managePage/brandActivity/update";
    }

    /**
     * 进入到修改页面
     *
     * @param modelmap
     * @return
     */
    @RequestMapping(value = "initUpdate/{id}")
    public String initUpdate(
            ModelMap modelmap,
            @PathVariable(value = "id") Integer id,
            @ModelAttribute("message") String message) {
        BrandActivity brandActivity = (BrandActivity) brandActivityService.getObjectById(id);
        modelmap.put("brandActivity", brandActivity);
        modelmap.put("message", message);
        return "managePage/brandActivity/update";
    }

    /**
     * @param modelmap
     * @param redirectAttributes
     * @param
     * @param cnContent
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "update")
    public String update(
            ModelMap modelmap,
            HttpServletRequest request,
            RedirectAttributes redirectAttributes,
            @RequestParam(value = "brandActivityId", required = true) Integer brandActivityId,
            @RequestParam(value = "cnContent", required = true) String cnContent,
            @RequestParam(value = "cnName", required = true) String cnName) {
        BrandActivity brandActivity;
        if (brandActivityId != null) {
            brandActivity = (BrandActivity) brandActivityService.getObjectById(brandActivityId);

        } else {
            brandActivity = new BrandActivity();

        }

        String image = FileUpload.picvideoupload("upload/brandActivity", "fileInput", request);
        if (!org.apache.commons.lang3.StringUtils.isBlank(image)) {
            brandActivity.setImage("upload/brandActivity/" + image);
        }
        brandActivity.setCnName(cnName);
        brandActivity.setCnContent(cnContent);
        brandActivity.setCreateDate(new Date());
        brandActivityService.saveOrUpdate(brandActivity);
        OperationLog operationLog = new OperationLog();
        operationLog.setMiaoshu("执行成功！");
        operationLog.setOperationdetails("添加/修改品种推介\""+brandActivity.getCnName()+"\"");
        operationLog.setOperationtime(new Date().toString());
        operationLog.setSensitive("修改品种推介");
        String ip = IpAddressUtil.getIpAddress(request);
        operationLog.setIpadderse(ip);
        String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
        operationLog.setUsername(sysUser);
        operationLogService.save(operationLog);
        redirectAttributes.addFlashAttribute("message", "\"品牌活动\"的信息保存成功");
        return "redirect:/brandActivity/manage/list.html";
    }



    /**
     * 根据id 删除
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value="delete/{id}")
    public String delete(
            @PathVariable(value="id") Integer id,
            HttpServletRequest request,  RedirectAttributes redirectAttributes){
        BrandActivity brandActivity = (BrandActivity) brandActivityService.getObjectById(id);
        OperationLog operationLog = new OperationLog();
        operationLog.setMiaoshu("执行成功");
        operationLog.setOperationdetails("删除品种推介\""+brandActivity.getCnName()+"\"");
        operationLog.setOperationtime(new Date().toString());
        operationLog.setSensitive("品种推介");
        String ip = IpAddressUtil.getIpAddress(request);
        operationLog.setIpadderse(ip);
        String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
        operationLog.setUsername(sysUser);
        operationLogService.save(operationLog);
        brandActivityService.delete(brandActivity);

        redirectAttributes.addFlashAttribute("message", "删除成功");
        return "redirect:/brandActivity/manage/list.html";

    }
}
